package com.example.component.Window;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.MenuBar.Command;

@SuppressWarnings("serial")
public class FunFusionMenuBar extends MenuBar implements ValueChangeListener,
		Command {

	private List<ValueChangeListener> listeners = new ArrayList<ValueChangeListener>();
	private Map<String, Type> typeMap = new HashMap<String, Type>();
	
	public static enum Type { 
		SYSTEMUSER, EVENTUSERS, LOGOUT, LOGINASANOTHERUSER, CURRENTEVENTS, PASTEVENTS, FINANCE
	};
	
	public FunFusionMenuBar() {

		setStyleName("menu");

		// Map menu item with a type
		typeMap.put("System Users", Type.SYSTEMUSER);
		typeMap.put("Event Users / Members", Type.EVENTUSERS);
		typeMap.put("Logout", Type.LOGOUT);
		typeMap.put("Login As Another User", Type.LOGINASANOTHERUSER);
		typeMap.put("Current Events", Type.CURRENTEVENTS);
		typeMap.put("Past Events", Type.PASTEVENTS);
		typeMap.put("Finance", Type.FINANCE);

		// Create the User Management menu
		MenuBar.MenuItem users = addItem("User Management", null, null);
		@SuppressWarnings("unused")
		MenuBar.MenuItem newFile = users.addItem("System Users", null, this);
		@SuppressWarnings("unused")
		MenuBar.MenuItem openFile = users.addItem("Event Users / Members", null, this);


		// Create the Events
		MenuBar.MenuItem events = addItem("Events", null, null);
		@SuppressWarnings("unused")
		MenuBar.MenuItem currentEvents = events.addItem("Current Events", null, this);
		@SuppressWarnings("unused")
		MenuBar.MenuItem pastEvents = events.addItem("Past Events", null, this);


		// Create the Finance menu
		MenuBar.MenuItem money = addItem("Money", null, null);
		@SuppressWarnings("unused")
		MenuBar.MenuItem selectAll = money.addItem("Finance", null, this);
		
		// This menu is for performing kinds of Logouts / Logins
		MenuBar.MenuItem logout = addItem("User Operation", null, null);
		@SuppressWarnings("unused")
		MenuBar.MenuItem logoutCompletely = logout.addItem("Logout", null, this);
		@SuppressWarnings("unused")
		MenuBar.MenuItem logoutAndLoginAsDifferentUser = logout.addItem("Login As Another User", null, this);
		
	}

	public void setImageToolsEnabled(boolean enabled){
		
		
	}

	
	public void addListener(ValueChangeListener listener) {
		listeners.add(listener);
	}
	
	public void removeListener(ClickListener listener) {
		listeners.remove(listener);
	}
	
	public void valueChange(ValueChangeEvent event) {
		for(ValueChangeListener listener : listeners){					
			listener.valueChange(event);
		}
		
	}

	public void menuSelected(MenuItem selectedItem) {
		
		final Type type = typeMap.get(selectedItem.getText());
		
		ValueChangeEvent evnt = new Property.ValueChangeEvent(){
			private static final long serialVersionUID = 1L;

			public Property getProperty() {				
				return new ObjectProperty(type);				
			}			
		};
		
		valueChange(evnt);
	}

}
